﻿using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Yz.Core.Tools;

namespace Yz.Core.Extensions
{
    public static class JsonExtension
    {

        public static string ToJsonStr(this object obj)
        {
            var jso = new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                // WriteIndented = true
            };
            jso.Converters.Add(new YzJsonConverter.DateTimeConverter());
            jso.Converters.Add(new YzJsonConverter.DateTimeNullableConverter());

            return JsonSerializer.Serialize(obj, jso);
        }

        public static T ToJsonObject<T>(this string str)
        {
            var jso = new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
                // WriteIndented = true
            };
            jso.Converters.Add(new YzJsonConverter.DateTimeConverter());
            jso.Converters.Add(new YzJsonConverter.DateTimeNullableConverter());

            return JsonSerializer.Deserialize<T>(str, jso);
        }
    }
}
